package edu.sysu.cshare.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import edu.sysu.cshare.model.Course;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.service.CourseService;
import edu.sysu.cshare.service.UserService;
import edu.sysu.cshare.util.Mis;

@Controller
@Scope("prototype")
public class SystemAction extends ActionSupport {

	private UserService userService;
	private CourseService courseService;
	private String userId;
	private User user;
	private List<Course> courses;
	private List<Long> selectedCourses;
	private long courseId;
	private String oldPassword;
	private String newPassword;
	private String confirmPassword;
	private String oldPasswordMsg;
	private String confirmPasswordMsg;

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Resource
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Course> getCourses() {
		return courses;
	}

	public List<Long> getSelectedCourses() {
		return selectedCourses;
	}

	public void setCourseId(long courseId) {
		this.courseId = courseId;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getOldPasswordMsg() {
		return oldPasswordMsg;
	}

	public String getConfirmPasswordMsg() {
		return confirmPasswordMsg;
	}

	public String viewSystem() {
		user = userService.get(userId);
		courses = courseService.loadAll();
		selectedCourses = new ArrayList<Long>();
		Iterator i = user.getCourses().iterator();
		while (i.hasNext())
			selectedCourses.add(((Course) i.next()).getId());
		return SUCCESS;
	}

	public String viewPersonal() {
		user = userService.get(userId);
		return INPUT;
	}

	public String select() {
		user = userService.get(userId);
		user.getCourses().add(courseService.get(courseId));
		courses = courseService.loadAll();
		selectedCourses = new ArrayList<Long>();
		Iterator i = user.getCourses().iterator();
		while (i.hasNext())
			selectedCourses.add(((Course) i.next()).getId());
		return SUCCESS;
	}

	public String disSelect() {
		user = userService.get(userId);
		for (Course c : user.getCourses())
			if (c.getId() == courseId) {
				user.getCourses().remove(c);
				break;
			}
		courses = courseService.loadAll();
		selectedCourses = new ArrayList<Long>();
		Iterator i = user.getCourses().iterator();
		while (i.hasNext())
			selectedCourses.add(((Course) i.next()).getId());
		return SUCCESS;
	}

	public String modifyPersonal() {
		User u = user;
		user = userService.get(userId);
		user.setNickname(u.getNickname());
		user.setDescrip(u.getDescrip());
		courses = courseService.loadAll();
		selectedCourses = new ArrayList<Long>();
		Iterator i = user.getCourses().iterator();
		while (i.hasNext())
			selectedCourses.add(((Course) i.next()).getId());
		if (oldPassword != null && !oldPassword.equals("")) {
			if (!Mis.getMD5(oldPassword).equals(user.getPassword())) {
				oldPasswordMsg = "密码不正确";
				return INPUT;
			} else if (!newPassword.equals(confirmPassword)) {
				confirmPasswordMsg = "两次输入的密码不同";
				return INPUT;
			} else
				user.setPassword(Mis.getMD5(newPassword));
		}
		userService.update(user);
		return SUCCESS;
	}
}
